import { AlipaySdk } from 'alipay-sdk';
// const { AlipaySdk } = require('alipay-sdk');
const fs = require('fs');

class AlipayFun {
    constructor(options) {
        this.options = options || {};
        this.appid = process.env.ALI_PAY_APP_KEY || '2021004149646090'
        this.alipaySdk = new AlipaySdk({
            appId: this.appid, //'2021004149646090',
            privateKey: fs.readFileSync(__dirname + `/alifile${this.appid}/alisiyao.txt`, 'ascii'),
            // 传入支付宝根证书、支付宝公钥证书和应用公钥证书。
            alipayRootCertPath: __dirname + `/alifile${this.appid}/alipayRootCert.crt`,
            alipayPublicCertPath: __dirname + `/alifile${this.appid}/alipayCertPublicKey_RSA2.crt`,
            appCertPath: __dirname + `/alifile${this.appid}/appCertPublicKey_${this.appid}.crt`,
            keyType: 'PKCS8'
        });
    }
    pay(options = {}) {
        const {
            out_trade_no,
            subject,
            total_amount,
            qr_pay_mode = '',
            junp_type = true,
            qrcode_width = 100,
            goods_detail = null,
            store_id = '', // 可作为记录代理使用
            invoice_info = null, // 后期可能开票需要使用
            returnUrl = 'https://doulingyang.cn', // 回调地址
        } = options
        // console.log('options', options)
        if (!out_trade_no || !subject || !total_amount) {
            return {
                code: -1,
                msg: '缺少必要参数'
            }
        }
        if (out_trade_no.length > 64) {
            return {
                code: -1,
                msg: 'out_trade_no长度不能超过64位'
            }
        }
        const bizContent = {
            out_trade_no,
            product_code: "FAST_INSTANT_TRADE_PAY",
            subject,
            // body: "234",
            total_amount,
            qr_pay_mode,
            qrcode_width: qr_pay_mode && qr_pay_mode == '4' ? qrcode_width : '',
            goods_detail,
            store_id,
            invoice_info,
            timeout_express: '2h',
        };

        for (let key in bizContent) {
            if (bizContent[key] == '' || bizContent[key] == null) {
                delete bizContent[key]
            }
        }
        // console.log('bizContent', bizContent)
        const html = this.alipaySdk.pageExec('alipay.trade.page.pay', junp_type ? 'GET' : 'POST', {
            bizContent,
            returnUrl,
            notify_url: process.env.NOTIFY_URL
        });
        return {
            code: 0,
            msg: 'success',
            data: html,
            out_trade_no
        }
    }
    checkNotifySign(options = {}) {
        const success = this.alipaySdk.checkNotifySignV2(options)

        if (success) {

            return {
                code: 0,
                msg: 'success',
                data: options
            }
        } else {
            return {
                code: 1,
                msg: 'fail',
                data: options
            }
        }
    }
    // 查询支付结果
    async query(options = {}) {
        const {
            out_trade_no,
            trade_no,
        } = options
        if (!out_trade_no && !trade_no) {
            return {
                code: -1,
                msg: '缺少必要参数'
            }
        }
        const bizContent = {
            out_trade_no,
            trade_no,
        };
        for (let key in bizContent) {
            if (bizContent[key] == '' || bizContent[key] == null) {
                delete bizContent[key]
            }
        }
        const html = await this.alipaySdk.exec('alipay.trade.query', { bizContent });
        return html
    }
    // 关闭订单
    async close(options = {}) {
        const {
            out_trade_no,
            trade_no,
        } = options
        if (!out_trade_no && !trade_no) {
            return {
                code: -1,
                msg: '缺少必要参数'
            }
        }
        const bizContent = {

            out_trade_no,
            trade_no,
        };
        for (let key in bizContent) {
            if (bizContent[key] == '' || bizContent[key] == null) {
                delete bizContent[key]
            }
        }
        const html = await this.alipaySdk.exec('alipay.trade.close', {
            bizContent,
            notify_url: process.env.NOTIFY_URL
        })
        return html
    }

}


export default AlipayFun;
